package main

import (
	"context"

	"log"
	"time"

	"github.com/grandcat/zeroconf"
)

func discover() {
	resolver, err := zeroconf.NewResolver(nil)
	if err != nil {
		log.Fatal(err)
	}

	entries := make(chan *zeroconf.ServiceEntry)
	go func(results <-chan *zeroconf.ServiceEntry) {
		for entry := range results {
			log.Printf("边缘网关发现节点: %s, %v:%d - %v:\n", entry.ServiceRecord.Instance, entry.AddrIPv4, entry.Port , entry.Text)
		}
	}(entries)

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
	defer cancel()

	err = resolver.Browse(ctx, "_http._tcp", "local.", entries)
	if err != nil {
		log.Fatal(err)
	}
	<-ctx.Done()
}


func main (){

	for {
		select {
		case <-time.After(10 * time.Second):
			 discover()

		case <-time.After(time.Second * 120):
			log.Println("重置时间 应该不会触发")
			return
		}
	}
}